$(function () {
    obj.init();
});

var obj = new function() {
    return {
        outAreaForm: null,
        form: null,
        init: function () {
            this.outAreaForm = $('#outArea_form');
            this.form = new xhForm(this.outAreaForm);
            // this.form.parse();
            this.funObj().addProvinceData();//获取select数据,填充缓存数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#province, #stage').unbind('change').on('change',function () {
                $this.funObj().addCityData();
            });
        },
        funObj: function () {
            var form = this.outAreaForm;
            return {
                addProvinceData: function () {
                    var stage = window.top.xhTop.getActiveFrame().obj.noteStage;
                    var prov = window.top.xhTop.getActiveFrame().obj.noteProv;
                    xhExtra.ajaxRequest('res/getProvinceData', {}, {check:true}, function (data) {
                        provObj = $('#province').select2({placeholder: '请选择', data: data, allowClear: false});
                        if (stage && prov){
                            provObj.val([prov]).trigger('change');
                        }
                    });
                    xhExtra.ajaxRequest('res/getStageData', {}, {check:true}, function (data) {
                        stageObj = $('#stage').select2({placeholder: '请选择', data: data, allowClear: false});
                        if (stage && prov){
                            stageObj.val([stage]).trigger('change');
                        }
                    });
                    var name = window.top.xhTop.getActiveFrame().obj.noteName;
                    var num = window.top.xhTop.getActiveFrame().obj.noteNum;
                    var city = window.top.xhTop.getActiveFrame().obj.noteCity;
                    if (name && num && city){
                        $('#name').html(name + '('+ num +')：');
                        form.find('#city').html(city);
                        document.getElementById('tr_1').style.display = 'none';
                        document.getElementById('tr_2').style.display = 'table';
                    }
                },
                addCityData: function () {
                    var province = form.find('#province').val();
                    var stage_data = form.find('#stage').val();
                    var province_name = '';
                    if (province !== ''){
                        province_name = form.find('#province').find("option:selected").text();
                    }
                    xhExtra.ajaxRequest('res/getCityData', {province:province,stage:stage_data,province_name:province_name}, {check:true}, function (data) {
                        if (data.status === 1){
                            $('#name').html(data.name + '('+ data.num +')：');
                            form.find('#city').html(data.city);
                            document.getElementById('tr_1').style.display = 'none';
                            document.getElementById('tr_2').style.display = 'table';
                            if (province && stage_data){
                                window.top.xhTop.getActiveFrame().obj.noteStage = stage_data;
                                window.top.xhTop.getActiveFrame().obj.noteProv = province;
                                window.top.xhTop.getActiveFrame().obj.noteName = data.name;
                                window.top.xhTop.getActiveFrame().obj.noteNum = data.num;
                                window.top.xhTop.getActiveFrame().obj.noteCity = data.city;
                            }
                        }
                    });
                }
            }
        }
    }
};
